﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing.Design;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Web.UI.WebControls;
using AjaxControlToolkit;
using SD.LLBLGen.Pro.ORMSupportClasses;

using VitalShining.WebControls;
using CARMSData.CollectionClasses;
using CARMSData.EntityClasses;
using CARMSData.HelperClasses;
using CommonToolkitScripts = AjaxControlToolkit.CommonToolkitScripts;

[assembly: WebResource("CARMS.Controls.PopupEditorBehavior.js", "text/javascript")]

namespace CARMS.Controls
{
    [ClientScriptResource("CARMS.PopupEditorBehavior", "CARMS.Controls.PopupEditorBehavior.js")]
    [RequiredScript(typeof(CommonToolkitScripts)), ControlValueProperty("Value"), ValidationProperty("Value")]
    public partial class PopupEditor : ScriptUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Width.IsEmpty)
                {
                    txtNumber.Width = Unit.Pixel(105);
                }
                else
                {
                    txtNumber.Width = Unit.Pixel(Convert.ToInt32(Width.Value) - 20);
                }
            }
        }
        public PopupEditor() :
            base(true, HtmlTextWriterTag.Div)
        {

        }

        [Browsable(false)]
        public Guid Value
        {
            get
            {
                object o = ViewState["Value"];
                return o == null ? Guid.Empty : (Guid)o;
            }
            set
            {
                if (Value != value)
                {
                    txtNumber.Text = GetText(value);
                }
                ViewState["Value"] = value;
            }
        }
        public string GetText(Guid value)
        {
            switch (Type)
            {
                case PopupType.Customer:
                    return GetCompanyName(value);
                case PopupType.ConventionCustomer:
                    return GetCompanyName(value);
                //case PopupType.Product:
                //    return GetProductDescription(value);
                //case PopupType.Engineer:
                //    return GetEmployeeNameByUserID(value);
                //case PopupType.Assistant:
                //    return GetEmployeeNameByUserID(value);
                //case PopupType.Inspector:
                //    return GetEmployeeNameByUserID(value);
                //case PopupType.Contactor:
                //    return GetContactorNameByContactorID(value);
                //case PopupType.Employee:
                //    return GetEmployeeNameByUserID(value);
                default:
                    return null;
            }
        }
        public Guid RelativeID
        {
            get
            {
                object o = ViewState["RelativeID"];
                return o == null ? Guid.Empty : (Guid)o;
            }
            set
            {
                ViewState["RelativeID"] = value;
            }
        }

        [Localizable(true), Category("Appearance"), DefaultValue("")]
        [ExtenderControlProperty]
        [ClientPropertyName("errorMessage")]
        public string ErrorMessage
        {
            get
            {
                object o = ViewState["ErrorMessage"];
                return o != null ? (string)o : string.Empty;
            }
            set
            {
                ViewState["ErrorMessage"] = value;
            }
        }

        [DefaultValue(PopupType.Customer)]
        public PopupType Type { get; set; }

        protected override void OnPreRender(EventArgs e)
        {
            if (RelativeID != Guid.Empty)
            {
                txtNumber_StyledAutoCompleteExtender.ContextKey = RelativeID.ToString();
            }
            else
            {
                txtNumber_StyledAutoCompleteExtender.ContextKey = Type.ToString();
            }

            base.OnPreRender(e);
        }

        protected override string SaveClientState()
        {
            return new JavaScriptSerializer().Serialize(
                new Dictionary<string, object>
                    {
                        {"Value", Value == Guid.Empty ? string.Empty : Value.ToString()},
                        {"TextElementID", txtNumber.ClientID},
                        {"ButtonElementID", ImgSelect.ClientID},
                        {"DialogUrl", GetDialogUrl()}
                    });
        }

        private string GetDialogUrl()
        {
            switch (Type)
            {
                case PopupType.Customer:
                    return ResolveUrl("~/Grids/CompanyList.aspx");
                case PopupType.ConventionCustomer:
                    return ResolveUrl("~/Grids/CompanyList.aspx?Type=ConventionCustomer");
                //case PopupType.Product:
                //    return ResolveUrl("~/Common/Products.aspx");
                //case PopupType.Engineer:
                //    return ResolveUrl("~/Common/Employees.aspx?Type=") + ConstUserRole.Engineer;
                //case PopupType.Assistant:
                //    return ResolveUrl("~/Common/Employees.aspx?Type=") + ConstUserRole.Assistant;

                //case PopupType.Inspector:
                //    return ResolveUrl("~/Common/Employees.aspx?Type=") + ConstUserRole.Inspector;
                //case PopupType.Contactor:
                //    return ResolveUrl("~/Common/Contactors.aspx?RelativeID=") + RelativeID;
                //case PopupType.Employee:
                //    return ResolveUrl("~/Common/Employees.aspx?ShowDistinct=true");
                default:
                    return string.Empty;
            }
        }

        private static Guid GetCompanyID(string companyName)
        {
            //var collection = new CustomerCollection();
            //collection.GetMulti(CustomerFields.FullName == companyName & CustomerFields.IsActive == true);
            //return collection.Count > 0 ? collection[0].CustomerId : Guid.Empty;
            return Guid.Empty;
        }

        private static string GetCompanyName(Guid companyID)
        {
            //var entity = new CustomerEntity(companyID);
            //return entity.IsNew ? null : entity.FullName;
            return string.Empty;
        }



        //private static Guid GetProductID(string prodcutDescription)
        //{
        //    var collection = new ViewProductCollection();
        //    collection.GetMulti(ViewProductFields.ProductName == prodcutDescription);
        //    return collection.Count > 0 ? collection[0].ProductId : Guid.Empty;
        //}

        //private static string GetProductDescription(Guid productID)
        //{
        //    var entity = new ProductEntity(productID);
        //    return entity.IsNew ? null : entity.ClientModelNumber + " " + entity.ProductDesignation;
        //}

        //private static Guid GetEmployeeUserID(string employeeName, PopupType type)
        //{

        //    var collection = new ViewEmployeeCollection();
        //    switch (type)
        //    {
        //        case PopupType.Engineer:
        //            collection.GetMulti(ViewEmployeeFields.EmployeeName == employeeName & ViewEmployeeFields.RoleId == new Guid(ConstUserRole.Engineer));
        //            break;
        //        case PopupType.Assistant:
        //            collection.GetMulti(ViewEmployeeFields.EmployeeName == employeeName & ViewEmployeeFields.RoleId == new Guid(ConstUserRole.Assistant));
        //            break;
        //        case PopupType.Inspector:
        //            collection.GetMulti(ViewEmployeeFields.EmployeeName == employeeName & ViewEmployeeFields.RoleId == new Guid(ConstUserRole.Inspector));
        //            break;
        //        case PopupType.Employee:
        //            collection.GetMulti(ViewEmployeeFields.EmployeeName == employeeName);
        //            break;
        //        default:
        //            collection.GetMulti(ViewEmployeeFields.EmployeeName == employeeName);
        //            break;
        //    }

        //    return collection.Count > 0 ? collection[0].UserId : Guid.Empty;
        //}

        //private Guid GetContactorID(string contactorName)
        //{
        //    var collection = new ViewContactorCollection();
        //    collection.GetMulti(ViewContactorFields.ContactorName == contactorName & ViewContactorFields.CompanyId == RelativeID & ViewContactorFields.IsActive == true);
        //    if (collection.Count > 0)
        //    {
        //        return collection[0].ContactorId;
        //    }
        //    else
        //    {
        //        return Guid.Empty;
        //    }
        //}

        //private static string GetEmployeeName(Guid employeeID)
        //{
        //    var entity = new VdeemployeeEntity(employeeID);
        //    return entity.IsNew ? null : entity.GivenName1 + " " + entity.SurName;
        //}

        //private static string GetEmployeeNameByUserID(Guid empUserID)
        //{
        //    var collection = new ViewEmployeeCollection();
        //    collection.GetMulti(ViewEmployeeFields.UserId == empUserID);
        //    return collection.Count > 0 ? collection[0].EmployeeName : "";
        //}

        //private static string GetContactorNameByContactorID(Guid contactorID)
        //{
        //    var collection = new ViewContactorCollection();
        //    collection.GetMulti(ViewContactorFields.ContactorId == contactorID & ViewContactorFields.IsActive == true);
        //    return collection.Count > 0 ? collection[0].ContactorName : "";
        //}


        [ExtenderControlMethod]
        public string GetValue(string text)
        {
            switch (Type)
            {
                case PopupType.Customer:
                    Guid id = GetCompanyID(text.Trim());
                    Value = id;
                    return id == Guid.Empty ? string.Empty : id.ToString();
                case PopupType.ConventionCustomer:
                    Guid idConventionCustomer = GetCompanyID(text.Trim());
                    Value = idConventionCustomer;
                    return idConventionCustomer == Guid.Empty ? string.Empty : idConventionCustomer.ToString();
                //case PopupType.Product:
                //    Guid idProduct = GetProductID(text);
                //    Value = idProduct;
                //    return idProduct == Guid.Empty ? string.Empty : idProduct.ToString();
                //case PopupType.Engineer:
                //    Guid idEngineer = GetEmployeeUserID(text, PopupType.Engineer);
                //    Value = idEngineer;
                //    return idEngineer == Guid.Empty ? string.Empty : idEngineer.ToString();
                //case PopupType.Assistant:
                //    Guid idAssistant = GetEmployeeUserID(text, PopupType.Assistant);
                //    Value = idAssistant;
                //    return idAssistant == Guid.Empty ? string.Empty : idAssistant.ToString();
                //case PopupType.Inspector:
                //    Guid idInspector = GetEmployeeUserID(text, PopupType.Inspector);
                //    Value = idInspector;
                //    return idInspector == Guid.Empty ? string.Empty : idInspector.ToString();
                //case PopupType.Contactor:
                //    Guid idContactor = GetContactorID(text);
                //    Value = idContactor;
                //    return idContactor == Guid.Empty ? string.Empty : idContactor.ToString();
                //case PopupType.Employee:
                //    Guid idEmployee = GetEmployeeUserID(text, PopupType.Employee);
                //    Value = idEmployee;
                //    return idEmployee == Guid.Empty ? string.Empty : idEmployee.ToString();

                default:
                    return null;
            }
        }
        protected override void LoadClientState(string clientState)
        {
            if (!Page.IsCallback)
            {
                if (clientState.Trim().Length == 36)
                {
                    Value =new Guid(clientState) ;
                }
            }
        }

        [DefaultValue("")]
        [ExtenderControlEvent]
        [ClientPropertyName("valueChanged")]
        [AjaxClientEventArguments("result", "Object")]
        [Editor("PT.ERP.Web.Design.ClientEventEditor, PT.ERP.Web.Design", typeof(UITypeEditor))]
        public virtual string OnClientValueChanged
        {
            get
            {
                return (string)ViewState["OnClientValueChanged"] ?? string.Empty;
            }
            set { ViewState["OnClientValueChanged"] = value; }
        }

        [ExtenderControlProperty, ClientPropertyName("enabled"), DefaultValue(true), Category("Appearance")]
        public bool Enabled
        {
            get
            {
                object o = ViewState["Enabled"];
                return o == null || (bool)o;
            }
            set
            {
                ViewState["Enabled"] = value;
            }
        }

        public string Text
        {
            get
            {
                return txtNumber.Text;
            }
            set
            {
                txtNumber.Text = value;
            }
        }
    }

    public enum PopupType
    {
        Customer,
        ConventionCustomer,
        //Product,
        //Engineer,
        //Inspector,
        //Contactor,
        //Employee,
        //Assistant
    }
}